package com.cryptape.cita_wallet.view;

import android.graphics.drawable.ColorDrawable;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.cryptape.cita_wallet.activity.EntrustActivity;
import com.cryptape.cita_wallet.activity.transfer.EntrustTransferPresenter;
import com.cryptape.cita_wallet.bean.AddDataItem;
import com.mangoworld.android.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dengming on 20/1/20.
 */

public class LockTypePopupWindow extends PopupWindow {

    private RecyclerView recyclerView;
    private List<AddDataItem> wallets;
    private AddDataPopupWindow.onClickImpl listener;
    private EntrustActivity activity;

    public LockTypePopupWindow(EntrustActivity activity, List<AddDataItem> data) {
        super(activity);
        this.activity = activity;
        this.wallets = new ArrayList<>();
        this.wallets.add(new AddDataItem("Nature", R.drawable.icon_scan));
        this.wallets.add(new AddDataItem("Mix", R.drawable.icon_scan));
        this.wallets.add(new AddDataItem("Empty",R.drawable.icon_new_note));
        this.listener = listener;
        View pop = LayoutInflater.from(activity).inflate(R.layout.popupwindow_pwd_unlock_wallet, null);
        recyclerView = pop.findViewById(R.id.recycler);
        this.setContentView(pop);
        this.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        this.setFocusable(true);
        ColorDrawable dw = new ColorDrawable(0x000000);
        this.setBackgroundDrawable(dw);
        recyclerView.setLayoutManager(new LinearLayoutManager(activity));
        Adapter adapter = new Adapter();
        recyclerView.setAdapter(adapter);

    }

    private class Adapter extends RecyclerView.Adapter<ViewHolder> {

        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_add_data, parent, false);
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
            holder.nameTv.setText(wallets.get(position).getName());
            holder.iconIv.setVisibility(View.GONE);
//            holder.nameTv.setOnClickListener((view) -> {
//                listener.click(wallets.get(position));
//                dismiss();
//            });
            holder.root.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(position == 0){
                        activity.setLockType(EntrustTransferPresenter.TYPE_LOCK_NATURE);
                    }else if(position == 1){
                        activity.setLockType(EntrustTransferPresenter.TYPE_LOCK_MIX);
                    }
                    else if(position == 2){
                        activity.setLockType(EntrustTransferPresenter.TYPE_LOCK_EMPTY);
                    }
                    dismiss();
                }
            });
        }

        @Override
        public int getItemCount() {
            return wallets.size();
        }
    }
    private static final int REQUEST_CODE = 0x01;
    private class ViewHolder extends RecyclerView.ViewHolder {
        private LinearLayout root;
        private TextView nameTv;
        private ImageView iconIv;

        public ViewHolder(View itemView) {
            super(itemView);
            nameTv = itemView.findViewById(R.id.tv_name);
            root = itemView.findViewById(R.id.root);
            iconIv = itemView.findViewById(R.id.icon);
        }
    }



    public interface onClickImpl {
        void click(AddDataItem wallet);
    }

}
